package com.yazif.vqa.api.wechat.controller;

import com.yazif.vqa.api.wechat.domain.Authentication;
import com.yazif.vqa.api.wechat.service.WechatService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class WechatController {

    @Resource
    private WechatService wechatService;

    @RequestMapping(value = "/api", method = {RequestMethod.GET})
    @ResponseBody
    public String identifyAuth(@RequestParam String signature, @RequestParam String timestamp, @RequestParam String nonce, @RequestParam String echostr){
        Authentication auth = new Authentication();
        auth.setSignature(signature);
        auth.setTimestamp(timestamp);
        auth.setNonce(nonce);
        auth.setEchostr(echostr);
        return wechatService.identifiyAuthentication(auth);
    }

    @RequestMapping(value = "/api", method = {RequestMethod.POST},  produces = "text/plain; charset=utf-8")
    @ResponseBody
    public String callback(@RequestBody String messageXml, HttpServletRequest request){
        return wechatService.callback(messageXml);
    }
}
